﻿/*
* This software is the confidential and proprietary
* information of NetBay
* Such confidential information should not be disclosed
* NetBay copyright(c)
*/
#region Using
using System;
using System.Collections.Generic;
using Rio.Flow.Business.Repository.Translator;
using Rio.Flow.DataAccess;
using NetBay.Core.BusinessEntities.Workflow;

#endregion

namespace Rio.Flow.Business.Repository
{
    /// <summary>
    /// Message repository
    /// </summary>
    public class WorkflowModelRepository
    {
        /// <summary>
        /// Returns all workflow models
        /// </summary>
        /// <returns></returns>
        public static List<WorkflowModel> GetWorkflowModel()
        {
            var models = WorkflowModelTranslator.TranslateWorkflowModels(DA_WorkflowModel.GetWorkflowModels());

            // add actions & conditions
            foreach (WorkflowModel mod in models)
            {
                var model = mod;
                AddActionsConditions(ref model);
            }

            return models;
        }

        /// <summary>
        /// Returns a model by this id
        /// </summary>
        /// <returns></returns>
        public static WorkflowModel GetWorkflowModelById(long wkfModelId)
        {
            var model = WorkflowModelTranslator.TranslateWorkflowModel(DA_WorkflowModel.GetWorkflowModelById(wkfModelId));
            AddActionsConditions(ref model);
            return model;
        }

        /// <summary>
        /// Returns a model by foldertype id
        /// </summary>
        /// <returns></returns>
        public static WorkflowModel GetWorkflowModelByName(string wkfModelName)
        {
            var model = new WorkflowModel();
            model = WorkflowModelTranslator.TranslateWorkflowModel(DA_WorkflowModel.GetWorkflowModelByName(wkfModelName));

            // add actions & conditions
            AddActionsConditions(ref model);

            return model;
        }

        /// <summary>
        /// Returns all actions for a model
        /// </summary>
        /// <returns></returns>
        public static List<WorkflowAction> GetModelActions(long wkfModelID)
        {
            return WorkflowModelTranslator.TranslateActionsOrConditions(DA_WorkflowModel.GetActions(wkfModelID));
        }

        /// <summary>
        /// Gets the color of the workflow state.
        /// </summary>
        /// <returns></returns>
        public static List<WorkflowStateInfo> GetWorkflowStateInfos()
        {
            return DA_WorkflowModel.GetWorkflowStateInfos();
        }

        /// <summary>
        /// Gets all actions.
        /// </summary>
        /// <returns></returns>
        public static List<WorkflowAction> GetAllActions()
        {
            return WorkflowModelTranslator.TranslateActionsOrConditions(DA_WorkflowModel.GetAllActions());
        }

        /// <summary>
        /// Returns all conditions for a model
        /// </summary>
        /// <returns></returns>
        public static List<WorkflowCondition> GetModelConditions(long wkfModelID)
        {
            return WorkflowModelTranslator.TranslateConditions(DA_WorkflowModel.GetConditions(wkfModelID));
        }

        /// <summary>
        /// Add a model
        /// </summary>
        /// <param name="model">Workflow definition instance</param>
        /// <param name="actions">List of workflow actions</param>
        /// <param name="conditions">List of workflow conditions</param>
        /// <returns></returns>
        public static bool AddWorkflowModel(WorkflowModel model, List<long> actions, List<long> conditions)
        {
            return DA_WorkflowModel.AddWorkflowModel(model, actions, conditions);
        }

        /// <summary>
        /// Add an action or a condition to the model
        /// </summary>
        /// <param name="wkfDefId">Workflow definition identifier</param>
        /// <param name="wkfActCondId">Workflow action identifier</param>
        /// <returns></returns>
        public static bool AddActionOrConditionToModel(long wkfDefId, long wkfActCondId)
        {
            return DA_WorkflowModel.AddActionOrConditionToModel(wkfDefId, wkfActCondId);
        }

        /// <summary>
        /// Add an action to the model
        /// </summary>
        /// <param name="wkfDefId">Workflow model identifier</param>
        /// <param name="wkfAction">Workflow action type</param>
        /// <returns></returns>
        public static bool AddActionToModel(long wkfDefId, long wkfAction)
        {
            return DA_WorkflowModel.AddActionOrConditionToModel(wkfDefId, Convert.ToInt32(wkfAction));
        }

        /// <summary>
        /// Add a condition to the model
        /// </summary>
        /// <param name="wkfDefId">Workflow model identifier</param>
        /// <param name="wkfAction">Workflow condition type</param>
        /// <returns></returns>
        public static bool AddConditionToModel(long wkfDefId, long wkfCondition)
        {
            return DA_WorkflowModel.AddActionOrConditionToModel(wkfDefId, Convert.ToInt32(wkfCondition));
        }

        /// <summary>
        /// Deletes the model.
        /// </summary>
        /// <param name="wkfModelId">The WKF model id.</param>
        /// <returns></returns>
        public static bool DeleteModel(long wkfModelId)
        {
            return DA_WorkflowModel.DeleteModel(wkfModelId);
        }

        /// <summary>
        /// Add actions and conditions to the model
        /// </summary>
        /// <param name="model"></param>
        private static void AddActionsConditions(ref WorkflowModel model)
        {
            // add & sort actions & conditions
            model.Actions = GetModelActions(model.Identifier);
            model.Actions.Sort((a, b) => a.Name.CompareTo(b.Name));

            model.Conditions = GetModelConditions(model.Identifier);
            model.Conditions.Sort((a, b) => a.Name.CompareTo(b.Name));
        }
    }
}